<div id="pointsRulePage" class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">积分规则</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>积分规则</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <form class="layui-form toolbar" name="queryForm">
            类型：
            <select name="prtype">
                <option value="">请选择规则类型</option>
            </select>&emsp;
            描述：
            <input name="porule" class="layui-input search-input enterSubmit" type="text"
                   placeholder="输入规则描述关键字"/>&emsp;
            <button id="pointsRule-btn-search" class="layui-btn icon-btn" lay-submit lay-filter="pointsRule-query"><i
                    class="layui-icon">&#xe615;</i>搜索
            </button>

            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            <button id="pointsRule-btn-add" type="button" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加
            </button>
        </form>

        <!-- 数据表格 -->
        <table class="layui-table" id="points-table" lay-filter="points-table"></table>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="points-state">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script>
    layui.use(['form', 'table', 'util', 'config', 'admin'], function () {
        let form = layui.form;
        let table = layui.table;
        let config = layui.config;
        let layer = layui.layer;
        let util = layui.util;
        let admin = layui.admin;


        let url = config.base_server + 'api-points/pointsRule/getPointsRule';
        let params = {
            access_token: config.getToken().access_token
        };
        let cols = [[
            {type: 'numbers'},
            {field: 'prcode', sort: true, title: '规则编号', align: 'center'},
            {field: 'prtyne', sort: true, title: '规则类型', align: 'center'},
            {field: 'porule', sort: true, title: '规则描述', align: 'center'},
            {field: 'points', sort: true, title: '积分值', align: 'center'},
            {
                field: 'cretim', sort: true, align: 'center', templet: function (d) {
                    return util.toDateString(d.cretim);
                }, title: '创建时间'
            },
            {
                field: 'updtim', sort: true, align: 'center', templet: function (d) {
                    return util.toDateString(d.updtim);
                }, title: '修改时间'
            },
            {templet: '#points-state', width: 120, align: 'center', title: '操作'}
        ]];

        // 渲染表格
        table.render({
            elem: '#points-table',
            url: url,
            where: params,
            page: true,
            cols: cols
        });

        // 工具条点击事件
        table.on('tool(points-table)', function (obj) {
            let data = obj.data;
            let layEvent = obj.event;
            if (layEvent === 'edit') {
                showEditModel(data);
            } else if (layEvent === 'del') {
                layer.confirm('此操作不可逆，确定删除吗？', function () {
                    layer.load(2);
                    admin.req('api-points/pointsRule/' + obj.data.poruid, {}, function (data) {
                        layer.closeAll('loading');
                        if (data.code === 0) {
                            layer.msg(data.msg, {icon: 1, time: 500});
                            table.reload('points-table', {});
                        } else {
                            layer.msg(data.msg, {icon: 2, time: 500});
                        }
                    }, 'DELETE');

                });
            }
        });

        // 填充下拉列表 - 积分规则类型
        admin.req('api-points/pointsRuleType/get', {}, (res) => {
            admin.putTempData('t_pointsRuleType',res.data);
            $.each(res.data, function (index, item) {
                let option = new Option(item.prtyne, item.prtycd);
                $("#pointsRulePage select[name='prtype']").append(option);
            });
            form.render();
        }, 'GET');

        // 添加按钮点击事件
        $('#pointsRule-btn-add').click(function () {
            showEditModel();
        });

        //显示表单弹窗
        let showEditModel = function (data) {
            admin.putTempData('t_pointsRule', data);
            let title = data ? '修改积分规则' : '添加积分规则';
            admin.popupCenter({
                title: title,
                path: 'pages/points/pointsRule_form.html',
                area: '800px',
                offset: '200px',
                finish: () => {
                    table.reload('points-table', {});
                    admin.putTempData('t_pointsRule', null);
                }, cancel: () => {
                    admin.putTempData('t_pointsRule', null);
                }
            });
        };

        // 条件检索
        form.on('submit(pointsRule-query)', (data) => {
            table.reload('points-table', {where: data.field});
            return false;
        });

        // 回车查询
        $('#pointsRulePage .enterSubmit').keyup((e) => {
            switch (e.keyCode) {
                case 13:
                    $('#pointsRule-btn-search').click();
                    break;
            }
        });
    });
</script>